package com.lsh.controller;

import com.github.pagehelper.PageInfo;
import com.lsh.domain.ArticlePage;
import com.lsh.domain.UserComment;
import com.lsh.domain.UserReply;
import com.lsh.service.ArticleService;
import com.lsh.service.CommentService;
import com.lsh.service.IndexService;
import com.lsh.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 我的管理模块
 */
@Controller
@RequestMapping("/user")
public class ManageController {

    @Autowired
    ArticleService articleService;
    @Autowired
    IndexService indexService;
    @Autowired
    CommentService commentService;

    /**
     * 首页（文章管理【全部】）
     * @param mv
     * @return
     */
    @RequestMapping({"/manage/index", "/article/manage/all"})
    public ModelAndView manageIndex(@CookieValue("TOKEN") String token, ModelAndView mv){
        String nickname = indexService.findNickname(token);
        Map map = articleService.articleIndex(token);
        mv.addObject("nickname", nickname);
        mv.addObject("year_month", map);
        mv.setViewName("/user/article/manage/article_all");
        return mv;
    }

    /**
     * 文章管理【草稿箱】
     * @param token
     * @param mv
     * @return
     */
    @RequestMapping("/article/manage/drafts")
    public ModelAndView manageDraft(@CookieValue("TOKEN") String token, ModelAndView mv){
        String nickname = indexService.findNickname(token);
        mv.addObject("nickname", nickname);
        mv.setViewName("/user/article/manage/article_drafts");
        return mv;
    }

    /**
     * 分页-文章管理【全部】
     * @return
     */
    @RequestMapping("/article/manage/all/{page}")
    public ModelAndView allArticlePage(@CookieValue("TOKEN") String token, @PathVariable("page") int page,
                                    ArticlePage articlePage, ModelAndView mv) throws UnsupportedEncodingException {
        String result = articleService.findAllArticlePage(token, page, articlePage);
        mv.addObject("data", result);
        mv.setViewName("/JSON");
        return mv;
    }

    /**
     * 分页-文章管理【草稿箱】
     * @return
     */
    @RequestMapping("/article/manage/drafts/{page}")
    public ModelAndView draftsArticlePage(@CookieValue("TOKEN") String token, @PathVariable("page") int page,
                                    ArticlePage articlePage, ModelAndView mv) throws UnsupportedEncodingException {
        String result = articleService.findDraftsArticlePage(token, page);
        mv.addObject("data", result);
        mv.setViewName("/JSON");
        return mv;
    }

    /**
     * 文章管理-删除
     * @return
     */
    @RequestMapping("/article/manage/delete/{artId}")
    public ModelAndView deleteArticle(@CookieValue("TOKEN") String token, @PathVariable("artId") String artId,
                                      @RequestParam("draft") int draft, ModelAndView mv){
        String result = articleService.deleteArticle(token, artId, draft);
        mv.addObject("data", result);
        mv.setViewName("/JSON");
        return mv;
    }

    /**
     * 评价管理 【我发表的评价】
     * @param token
     * @param mv
     * @return
     */
    @GetMapping("/comment/manage/comments")
    public ModelAndView manageComment(@CookieValue("TOKEN") String token, ModelAndView mv){
        String nickname = indexService.findNickname(token);
        mv.addObject("nickname", nickname);
        mv.setViewName("/user/article/manage/article_comment");
        return mv;
    }

    /**
     * 评价管理 【我发表的回复】
     * @param token
     * @param mv
     * @return
     */
    @GetMapping("/comment/manage/replys")
    public ModelAndView manageReply(@CookieValue("TOKEN") String token, ModelAndView mv){
        String nickname = indexService.findNickname(token);
        mv.addObject("nickname", nickname);
        mv.setViewName("/user/article/manage/article_reply");
        return mv;
    }

    /**
     * 请求获取用户评价（分页）
     * @param token
     * @return
     */
    @GetMapping(value = {"/comment/manage/c/{page}", "/comment/manage/c"})
    @ResponseBody
    public Result getAllComment(@CookieValue("TOKEN") String token, @PathVariable(value = "page", required = false) Integer pageNum){
        String userId = indexService.findByUserId(token);
        pageNum = (pageNum == null) ? 1 : pageNum;
        PageInfo commentPage = commentService.findCommentPage(pageNum, userId);
        return new Result(1001, "请求成功", new HashMap<>()).addExtraData("result", commentPage);
    }

    /**
     * 请求获取用户回复（分页）
     * @param token
     * @return
     */
    @GetMapping(value = {"/comment/manage/r/{page}","/comment/manage/r"})
    @ResponseBody
    public Result getAllReply(@CookieValue("TOKEN") String token, @PathVariable(value = "page", required = false) Integer pageNum){
        String userId = indexService.findByUserId(token);
        pageNum = (pageNum == null) ? 1 : pageNum;
        PageInfo replyPage = commentService.findReplyPage(pageNum, userId);
        return new Result(1001, "请求成功", new HashMap<>()).addExtraData("result", replyPage);
    }

    /**
     * 根据id删除对应评价
     * @param token
     * @param commentId
     * @return
     */
    @DeleteMapping("/comment/manage/c/{id}")
    @ResponseBody
    public Result removeCommentById(@CookieValue("TOKEN") String token, @PathVariable("id") String commentId){
        String userId = indexService.findByUserId(token);
        int result = commentService.deleteCommentById(commentId, userId);
        if(result <= 0) return new Result(1002, "删除失败");
        return new Result(1001, "删除成功");
    }

    /**
     * 根据id删除对应回复
     * @param token
     * @param replyId
     * @return
     */
    @DeleteMapping("/comment/manage/r/{id}")
    @ResponseBody
    public Result removeReplyById(@CookieValue("TOKEN") String token, @PathVariable("id") String replyId){
        String userId = indexService.findByUserId(token);
        int result = commentService.deleteReplyById(replyId, userId);
        if(result <= 0) return new Result(1002, "删除失败");
        return new Result(1001, "删除成功");
    }
}
